package com.bst.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bst.base.domain.BaseReportCacheData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bst.common.annotation.Log;
import org.springframework.stereotype.Controller;
import com.bst.common.enums.BusinessType;
import com.bst.base.domain.BaseReportCacheLock;
import com.bst.base.service.IBaseReportCacheLockService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.core.page.TableDataInfo;

/**
 * 缓存锁定Controller
 * 
 * @author laoliu
 * @date 2024-12-31
 */
@Controller
@RequestMapping("/base/report/cache/lock")
public class BaseReportCacheLockController extends BaseController
{
    private String prefix = "base/lock";

    @Autowired
    private IBaseReportCacheLockService baseReportCacheLockService;



    @Log(title = "报表数据缓存", businessType = BusinessType.QUERY)
    @GetMapping( "/selectLockDataByPids")
    @ResponseBody
    public AjaxResult selectLockDataByPids(String pids,String cDate,String dim)
    {
        List<BaseReportCacheLock> list = baseReportCacheLockService.selectLockDataByPids(pids.split(","),cDate);
        Map<Long,BaseReportCacheLock> dataMap = new HashMap<>();
        for (BaseReportCacheLock data: list) {
            dataMap.put(data.getIdReportCache(),data);
        }
        return AjaxResult.success(dataMap);
    }

    @Log(title = "缓存数据锁定", businessType = BusinessType.UPDATE)
    @GetMapping("/lock")
    @ResponseBody
    public AjaxResult lock(Long idReportCache,String cDate) {
        BaseReportCacheLock lock = new BaseReportCacheLock();
        lock.setIdReportCache(idReportCache);
        lock.setcDate(cDate);
        List<BaseReportCacheLock> list = baseReportCacheLockService.selectBaseReportCacheLockList(lock);
        if(list.size()==0) {
            baseReportCacheLockService.insertBaseReportCacheLock(lock);
        }
        return AjaxResult.success(lock);
    }

    @Log(title = "缓存数据解锁", businessType = BusinessType.UPDATE)
    @GetMapping("/unlock")
    @ResponseBody
    public AjaxResult unlock(Long idReportCache,String cDate) {
        BaseReportCacheLock lock = new BaseReportCacheLock();
        lock.setIdReportCache(idReportCache);
        lock.setcDate(cDate);
        List<BaseReportCacheLock> list = baseReportCacheLockService.selectBaseReportCacheLockList(lock);
        if(list.size()>0) {
            String ids = "";
            for (BaseReportCacheLock baseReportCacheLock : list) {
                ids += baseReportCacheLock.getIdReportCacheLock()+",";
            }
            baseReportCacheLockService.deleteBaseReportCacheLockByIdReportCacheLocks(ids.substring(0,ids.length()-1));
        }
        return AjaxResult.success(lock);
    }
}
